package com.zy.fee.roadpark.activity.np;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.adapter.NpNodeAdapter;
import com.zy.fee.roadpark.config.Constants;
import com.zy.fee.roadpark.dao.NpNodeLogic;
import com.zy.fee.roadpark.model.M_Node;
import com.zy.fee.roadpark.model.apimodel.APIM_NodeList;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.PreferencesHelper;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wikison on 2018/9/12.
 * 车位列表
 */
public class NpSpotListActivity extends BaseActivity implements NpNodeAdapter.OnItemClickListener {
  public static final String INTENT_NODE = "INTENT_NODE";

  @BindView(R.id.iv_right) ImageView ivRight;
  @BindView(R.id.lh_tv_title) TextView lhTvTitle;
  @BindView(R.id.tv_spot_num) TextView tvSpotNum;
  @BindView(R.id.recyclerView) SuperRecyclerView recyclerView;

  private NpNodeAdapter nodeAdapter;
  private List<M_Node> nodeList = new ArrayList<>();

  @Override public void setContentView() {
    setContentView(R.layout.activity_spot_list);
  }

  @Override public void init() {
    registerReceiver(new String[] { Constants.BROADCAST_REFRESH_NODE_LIST, Constants.BROADCAST_DIALOG_PAY });
    initData();
    initView();
    initListener();
  }

  private void initData() {
    nodeAdapter = new NpNodeAdapter(mContext, nodeList, false);
    nodeAdapter.setOnItemClickListener(this);

    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    recyclerView.setAdapter(nodeAdapter);

    node_num();
    node_list();
  }

  private void initView() {
    lhTvTitle.setText("车位列表");

    //ivRight.setImageResource(R.mipmap.icon_filter);
  }

  private void initListener() {
    recyclerView.setRefreshListener(() -> node_list());
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override protected void handleReceiver(Context context, Intent intent) {
    super.handleReceiver(context, intent);
    if (intent == null || TextUtils.isEmpty(intent.getAction())) {
      return;
    }
    if (Constants.BROADCAST_REFRESH_NODE_LIST.equals(intent.getAction())) {
      node_num();
      node_list();
    }if (Constants.BROADCAST_DIALOG_PAY.equals(intent.getAction())) {
      node_num();
      node_list();
    }
  }

  @OnClick({
      R.id.ll_back
  }) public void onViewClicked(View view) {
    Intent intent;
    switch (view.getId()) {
      case R.id.ll_back:
        finish();
        break;
    }
  }

  @Override public void onItemClick(View view, M_Node m) {
    Intent intent = new Intent(mContext, NpNodeReceivablesActivity.class);
    intent.putExtra(INTENT_NODE, m);
    startActivity(intent);
  }

  private void node_num() {
    NpNodeLogic.npParkNum(mContext,
        preferencesHelper.getSettingString(PreferencesHelper.User.Key.LOT_ID),
        new HttpRequest.HttpCallback() {
          @Override public void onStart() {

          }

          @Override public void onSucceed(Object obj) {
            CommonResult result = (CommonResult) obj;
            tvSpotNum.setText(
                "车位总数：" + result.getTotalPark() + "      空闲车位：" + result.getFreePark());
          }

          @Override public void onFailed() {

          }

          @Override public void onFinish() {

          }
        });
  }

  private void node_list() {
    NpNodeLogic.node_list(mContext,
        preferencesHelper.getSettingString(PreferencesHelper.User.Key.LOT_ID), 1, 1000,
        new HttpRequest.HttpCallback() {
          @Override public void onStart() {
            showNoCancelPd();
          }

          @Override public void onSucceed(Object obj) {
            APIM_NodeList result = (APIM_NodeList) obj;

            nodeAdapter.setData(result.getNodeList(), false);
          }

          @Override public void onFailed() {

          }

          @Override public void onFinish() {
            dismissPd();
          }
        });
  }
}
